package com.zpp.wedding_admin.service.impl;

import com.zpp.wedding_admin.entity.Order;
import com.zpp.wedding_admin.mapper.OrderMapper;
import com.zpp.wedding_admin.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zpp.wedding_admin.utils.Convert;
import com.zpp.wedding_admin.utils.OrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朱盼盼
 * @since 2022-12-29
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public boolean addOrder(Integer userId, Integer activityId) {
        Order order = new Order();
        String s = OrderUtils.getUUID().substring(0, 18);
        order.setOrderCode(s);
        order.setUserId(userId);
        order.setActivityId(activityId);
        order.setPrice(new Random().nextInt(2000) + 1000);
        order.setIsSolve(1);
        int i = orderMapper.insert(order);
        return i > 0;
    }

    @Override
    public List<Order> getOrderList(Integer id) {
        return orderMapper.getOrderList(id);
    }

    @Override
    public Map<String, Object> getByPage(Long currentPage, Long size) {
        int current = Convert.LongToInt(currentPage);
        int limit = Convert.LongToInt(size);
        Map<String, Object> map = new HashMap<>();
        List<Order> orderList = orderMapper.getByPage();
        List<Order> orderList1 = new ArrayList<>();
        for (int i = (current - 1) * limit; i < orderList.size() && i < current * limit; i++) {
            orderList1.add(orderList.get(i));
        }
        map.put("orderList", orderList1);
        map.put("total", Convert.IntToLang(orderList.size()));
        return map;
    }
}
